<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce, sortBy } from '@/utils'

const animationDuration = 1000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '450px'
    },
    chartData: {
      type: Object,
      required: true
    },
    chartSet: {
      type: Object,
      default: ()=>{}
    },
    chartType: {
      type: Array,
      required: true
    },
  },
  data() {
    return {
      chart: null,
      seriesData: null,
      series:[
        {
          //name: 'pageA',
          type: 'bar',
          //数据堆叠
          //stack: 'vistors',
          barWidth: '20%',
          barGap: '30%',
          animationDuration ,
          itemStyle:{
            normal:{
              color:"#93ed39"
            }
          },
        },
        {
          // name: 'pageB',
          type: 'line',
          itemStyle: {
            normal: {
              color: '#3282db',
              lineStyle: {
                color: '#3282db',
                width: 2
              },
            }
          },
          smooth: true,
          // stack: 'vistors',
          // barWidth: '60%',
          // data: [80, 52, 200, 334, 390, 330, 220],
          animationDuration
        }        
      ]
    }
  },
  watch:{
    chartData: {
      deep: true,
      handler(val) {
        this.initChart();
        this.setOptions(val,this.chartSet)
      }
    },
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOptions(dataset,{xName,yName}={}){
      this.chart.setOption({
        // tooltip: {
        //   trigger: 'axis',
        //   axisPointer: { // 坐标轴指示器，坐标轴触发有效
        //     type: 'cross', // 默认为直线，可选为：'line' | 'shadow'
        //     // axis: 'x',
        //   },
        //   padding: [5, 10]
        // },
        grid: {
          // top: 10,
          // left: '2%',
          // right: '2%',
          // bottom: '3%',
          left: 20,
          right: 50,
          bottom: 20,
          top: 50,
          containLabel: true
        },
        tooltip: {
            show: "true",
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
                type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            },
            formatter:function(params){
                // console.log(params)
                params.sort(sortBy("componentIndex"))
                // let res=params[0].name+"<br/>"+"儿童入场人数:"+params[0].data["儿童入场人数"]+"<br/>"+"儿童出场人数:"+params[0].data["儿童出场人数"]+"<br/>"+"儿童在场人数:"+params[0].data["儿童在场人数"]+"<br/>"+"总入场人数:"+params[0].data["总入场人数"]+"<br/>"+"总出场人数:"+params[0].data["总出场人数"]+"<br/>"+"总在场人数:"+params[0].data["总在场人数"]
                let res='<div class=tooltipStyle><div class=list><div class=listTitle>'+params[0].name+'</div>';
                for(let p of params){
                    //console.log(p)
                    res+='<div class=item><div class=itemCircle style="background:'+p.color+'"></div><div>'+p.seriesName+'：'+params[0].value[p.seriesName]+'</div></div>'
                }
                res+='</div></div>';
                return res;
            }
        },
        xAxis: [{
            type: 'category',
            name:xName?xName:"",
            //   nameLocation:"center",
            //   nameGap:30,
            z:1,
            splitLine:{show: false},
            //data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
            axisTick: {
                alignWithLabel: true
            }
        },{
            type: 'category',
            z:2,
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                show: false
            },
            splitArea: {
                show: false
            },
            splitLine: {
                show: false
            },
        }],
        yAxis: [{
          type: 'value',
          name:yName?yName:"",
          nameTextStyle:{
            padding:[0,0,0,20]
          },
          splitLine:{show: false},
          splitArea:{show: false},
          // axisLabel:{
          //   formatter: '{value}%'
          // },
          axisTick: {
            show: false
          }
        }],
        legend: {
            show:true,
            right:"10%",
            top:"3%",
        },
        dataset:dataset,
        series:this.seriesData,
      },true)
    },
    initChart() {
      this.seriesData=[];
      for(let val of this.chartType){
        if(val.type=="bar"){
          let obj=JSON.parse(JSON.stringify(this.series[0]))
          if(val.color){
            obj.itemStyle.normal.color=val.color;
          }
          if(val.barGap){
            obj.barGap=val.barGap;
          }
          if(val.z){
            obj.xAxisIndex=val.z;
          }
          this.seriesData.push(obj);
        }else if(val.type=="line"){
          let obj=JSON.parse(JSON.stringify(this.series[1]))
          if(val.color){
            obj.itemStyle.normal.color=val.color;
            obj.itemStyle.normal.lineStyle.color=val.color;
          }
          this.seriesData.push(obj);
        }
      }
      //console.log(this.seriesData)
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData,this.chartSet);
    },
  }
}
</script>
